﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IPR_StudyMeetingMrgSys.DBUtility;//Please add references
namespace IPR_StudyMeetingMrgSys.DAL
{
	/// <summary>
	/// 数据访问类:TrainContent
	/// </summary>
	public partial class TrainContent
	{
		public TrainContent()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(IPR_StudyMeetingMrgSys.Model.TrainContent model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TrainContent(");
			strSql.Append("TPID,TCID,TContent,TCDepID,IsCheck,IsRelease,AddUserID,AddUserName,AddDateTime,IsUse,TCDepLevel,TCSendDepID,TCOID,TCMark)");
			strSql.Append(" values (");
			strSql.Append("@TPID,@TCID,@TContent,@TCDepID,@IsCheck,@IsRelease,@AddUserID,@AddUserName,@AddDateTime,@IsUse,@TCDepLevel,@TCSendDepID,@TCOID,@TCMark)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@TPID", SqlDbType.BigInt,8),
					new SqlParameter("@TCID", SqlDbType.BigInt,8),
					new SqlParameter("@TContent", SqlDbType.NVarChar,500),
					new SqlParameter("@TCDepID", SqlDbType.Int,4),
					new SqlParameter("@IsCheck", SqlDbType.Int,4),
					new SqlParameter("@IsRelease", SqlDbType.Int,4),
					new SqlParameter("@AddUserID", SqlDbType.NVarChar,20),
					new SqlParameter("@AddUserName", SqlDbType.NVarChar,20),
					new SqlParameter("@AddDateTime", SqlDbType.NVarChar,20),
					new SqlParameter("@IsUse", SqlDbType.Int,4),
					new SqlParameter("@TCDepLevel", SqlDbType.Int,4),
                    new SqlParameter("@TCSendDepID", SqlDbType.NVarChar,300),

                    new SqlParameter("@TCOID", SqlDbType.BigInt,8),
                    new SqlParameter("@TCMark", SqlDbType.Int,4)};
			parameters[0].Value = model.TPID;
			parameters[1].Value = model.TCID;
			parameters[2].Value = model.TContent;
			parameters[3].Value = model.TCDepID;
			parameters[4].Value = model.IsCheck;
			parameters[5].Value = model.IsRelease;
			parameters[6].Value = model.AddUserID;
			parameters[7].Value = model.AddUserName;
			parameters[8].Value = model.AddDateTime;
			parameters[9].Value = model.IsUse;
            parameters[10].Value = model.TCDepLevel;
            parameters[11].Value = model.TCSendDepID;
            parameters[12].Value = model.TCOID;
            parameters[13].Value = model.TCMark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(IPR_StudyMeetingMrgSys.Model.TrainContent model, out string errorMsg)
        {
            bool isContinue = true; errorMsg = string.Empty;
            IPR_StudyMeetingMrgSys.Model.TrainPlan obj = new IPR_StudyMeetingMrgSys.Model.TrainPlan();
            StringBuilder strSql_select = new StringBuilder();
            strSql_select.Append("select  top 1 IsRelease,IsCheck from TrainContent ");
            strSql_select.Append(" where TCID=@TCID");
            SqlParameter[] parameters_select = {
                    new SqlParameter("@TCID", SqlDbType.BigInt)
            };
            model.IsUse = 1;
            parameters_select[0].Value = model.TCID;
            DataSet ds = DbHelperSQL.Query(strSql_select.ToString(), parameters_select);
            if (ds.Tables[0].Rows.Count > 0)
            {
                var item = ds.Tables[0].Rows[0];
                if (item["IsRelease"] != null && item["IsRelease"].ToString() != "")
                {
                    if (int.Parse(item["IsRelease"].ToString()) > 0)
                    {
                        isContinue = false; errorMsg = "已发布不能编辑";
                    }
                }
                if (item["IsRelease"] != null && item["IsRelease"].ToString() != "")
                {
                    if (int.Parse(item["IsCheck"].ToString()) > 0)
                    {
                        isContinue = false; errorMsg = "已审核不能编辑";
                    }
                }
            }
            if (isContinue)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update TrainContent set ");
                strSql.Append("TPID=@TPID,");
                strSql.Append("TCID=@TCID,");
                strSql.Append("TContent=@TContent,");
                //strSql.Append("TCDepID=@TCDepID,");
                //strSql.Append("IsCheck=@IsCheck,");
                //strSql.Append("IsRelease=@IsRelease,");
                strSql.Append("AddUserID=@AddUserID,");
                strSql.Append("AddUserName=@AddUserName,");
                strSql.Append("AddDateTime=@AddDateTime,");
                strSql.Append("IsUse=@IsUse,");
                //strSql.Append("TCDepLevel=@TCDepLevel,");
                strSql.Append("TCOID=@TCOID,");
                strSql.Append("TCMark=@TCMark,");
                strSql.Append("TCSendDepID=@TCSendDepID");
                strSql.Append(" where TCID=@TCID");
                SqlParameter[] parameters = {
                    new SqlParameter("@TPID", SqlDbType.BigInt,8),
                    new SqlParameter("@TCID", SqlDbType.BigInt,8),
                    new SqlParameter("@TContent", SqlDbType.NVarChar,500),
                    //new SqlParameter("@TCDepID", SqlDbType.Int,4),
                    //new SqlParameter("@IsCheck", SqlDbType.Int,4),
                    //new SqlParameter("@IsRelease", SqlDbType.Int,4),
                    new SqlParameter("@AddUserID", SqlDbType.NVarChar,20),
                    new SqlParameter("@AddUserName", SqlDbType.NVarChar,20),
                    new SqlParameter("@AddDateTime", SqlDbType.NVarChar,20),
                    new SqlParameter("@IsUse", SqlDbType.Int,4),
                    //new SqlParameter("@TCDepLevel", SqlDbType.Int,4),
                    new SqlParameter("@AutoID", SqlDbType.BigInt,8),
                    new SqlParameter("@TCSendDepID", SqlDbType.NVarChar,300),
                    new SqlParameter("@TCOID", SqlDbType.BigInt,8),
                    new SqlParameter("@TCMark", SqlDbType.Int,4)};
                parameters[0].Value = model.TPID;
                parameters[1].Value = model.TCID;
                parameters[2].Value = model.TContent;
                //parameters[3].Value = model.TCDepID;
                //parameters[3].Value = model.IsCheck;
                //parameters[4].Value = model.IsRelease;
                parameters[3].Value = model.AddUserID;
                parameters[4].Value = model.AddUserName;
                parameters[5].Value = model.AddDateTime;
                parameters[6].Value = model.IsUse;
                //parameters[10].Value = model.TCDepLevel;
                parameters[7].Value = model.AutoID;
                parameters[8].Value = model.TCSendDepID;
                parameters[9].Value = model.TCOID;
                parameters[10].Value = model.TCMark;
                int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                isContinue = rows > 0 ? true : false;
            }
            return isContinue;
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long TCID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TrainContent ");
			strSql.Append(" where TCID=@TCID");
			SqlParameter[] parameters = {
					new SqlParameter("@TCID", SqlDbType.BigInt)
			};
			parameters[0].Value = TCID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AutoIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TrainContent ");
			strSql.Append(" where TCID in (" + AutoIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.TrainContent GetModel(long TCID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AutoID,TPID,TCID,TContent,TCDepID,IsCheck,IsRelease,AddUserID,AddUserName,AddDateTime,IsUse,TCDepLevel,TCSendDepID,TCOID,TCMark from TrainContent ");
			strSql.Append(" where TCID=@TCID");
			SqlParameter[] parameters = {
					new SqlParameter("@TCID", SqlDbType.BigInt)
			};
			parameters[0].Value = TCID;

			IPR_StudyMeetingMrgSys.Model.TrainContent model=new IPR_StudyMeetingMrgSys.Model.TrainContent();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.TrainContent DataRowToModel(DataRow row)
		{
			IPR_StudyMeetingMrgSys.Model.TrainContent model=new IPR_StudyMeetingMrgSys.Model.TrainContent();
			if (row != null)
			{
				if(row["AutoID"]!=null && row["AutoID"].ToString()!="")
				{
					model.AutoID=long.Parse(row["AutoID"].ToString());
				}
				if(row["TPID"]!=null && row["TPID"].ToString()!="")
				{
					model.TPID=long.Parse(row["TPID"].ToString());
				}
				if(row["TCID"]!=null && row["TCID"].ToString()!="")
				{
					model.TCID=long.Parse(row["TCID"].ToString());
				}
				if(row["TContent"]!=null)
				{
					model.TContent=row["TContent"].ToString();
				}
				if((row["TCDepID"]!=null) && row["TCDepID"].ToString() != "")
				{
					model.TCDepID= int.Parse(row["TCDepID"].ToString());
				}
				if(row["IsCheck"]!=null && row["IsCheck"].ToString()!="")
				{
					model.IsCheck=int.Parse(row["IsCheck"].ToString());
				}
				if(row["IsRelease"]!=null && row["IsRelease"].ToString()!="")
				{
					model.IsRelease=int.Parse(row["IsRelease"].ToString());
				}
				if(row["AddUserID"]!=null)
				{
					model.AddUserID=row["AddUserID"].ToString();
				}
				if(row["AddUserName"]!=null)
				{
					model.AddUserName=row["AddUserName"].ToString();
				}
				if(row["AddDateTime"]!=null)
				{
					model.AddDateTime=row["AddDateTime"].ToString();
				}
				if(row["IsUse"]!=null && row["IsUse"].ToString()!="")
				{
					model.IsUse=int.Parse(row["IsUse"].ToString());
				}
				if(row["TCDepLevel"]!=null && row["TCDepLevel"].ToString()!="")
				{
					model.TCDepLevel=int.Parse(row["TCDepLevel"].ToString());
                }
                if (row["TCSendDepID"] != null)
                {
                    model.TCSendDepID = row["TCSendDepID"].ToString();
                }
                if (row["TCOID"] != null && row["TCOID"].ToString() != "")
                {
                    model.TCOID = long.Parse(row["TCOID"].ToString());
                }
                if (row["TCMark"] != null && row["TCMark"].ToString() != "")
                {
                    model.TCMark = int.Parse(row["TCMark"].ToString());
                }
            }
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AutoID,TPID,TCID,TContent,TCDepID,IsCheck,IsRelease,AddUserID,AddUserName,AddDateTime,IsUse,TCDepLevel,TCSendDepID,TCOID,TCMark ");
			strSql.Append(" FROM TrainContent ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AutoID,TPID,TCID,TContent,TCDepID,IsCheck,IsRelease,AddUserID,AddUserName,AddDateTime,IsUse,TCDepLevel,TCSendDepID,TCOID,TCMark ");
			strSql.Append(" FROM TrainContent ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TrainContent ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AutoID desc");
			}
			strSql.Append(")AS Row, T.*  from TrainContent T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "TrainContent";
			parameters[1].Value = "AutoID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        /// <summary>
        /// 是否存在该记录,名称是否重复
        /// </summary>
        public bool Exists(string TContent)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from TrainContent");
            strSql.Append(" where TContent=@TContent");
            SqlParameter[] parameters = {
                    new SqlParameter("@TContent", SqlDbType.NVarChar,500)
            };
            parameters[0].Value = TContent;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex, out int rowCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AutoID desc");
            }
            strSql.Append(")AS Row, T.*  from TrainContent T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");

            rowCount = 0;
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null)
                if (ds.Tables.Count > 0)
                    if (ds.Tables[0].Rows.Count > 0)
                        rowCount = ds.Tables[0].Rows.Count;

            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 培训计划发布
        /// </summary>
        public bool Release(string TCIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update TrainContent set IsRelease=1 ");
            strSql.Append(" where TCID in (" + TCIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

